<?php
class Relations_Das extends Basic_Das {
	
	public function addRelation($data = array()) {
		if (! is_array ( $data )) {
			throw new Exception ( "Relations_Das: The parameter is not an array." );
		}
		$required = array ('HOSTID', 'FRIENDID' );
		if (! $this->_required ( $required, $data )) {
			throw new Exception ( "Relations_Das: Required field is missing." );
		}
		
		$data ['CREATETIME'] = date ( 'Y-m-d H:i:s' );
		
		$this->load->database ();
		$this->db->insert ( 'RELATIONS', $data );
		$this->db->close ();
	}
	
	public function getRelation($relation_id) {
		if (! is_int ( $relation_id )) {
			throw new Exception ( "Relations_Das: The parameter is not an int." );
		}
		
		$this->load->database ();
		
		$this->db->where ( 'RELATIONID', $relation_id );
		$this->db->from ( 'RELATIONS' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		return $query->result_array ();
	}
	
	public function getRelations($user_id) {
		if (! is_int ( $user_id )) {
			throw new Exception ( "Relations_Das: The parameter is not an int." );
		}
		
		$this->load->database ();
		
		$this->db->where ( 'USERID', $user_id );
		$this->db->from ( 'RELATIONS' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		return $query->result_array ();
	}
	
	public function deleteRelation($data = array()) {
		if (! is_array ( $data )) {
			throw new Exception ( "Relations_Das: The parameter is not an array." );
		}
		$required = array ('HOSTID', 'FRIENDID' );
		if (! $this->_required ( $required, $data )) {
			throw new Exception ( "Relations_Das: Required field is missing." );
		}
		
		$this->load->database ();
		$this->db->delete ( 'RELATIONS', array ('HOSTID' => $data ['HOSTID'], 'FRIENDID' => $data ['FRIENDID'] ) );
		$this->db->close ();
		
		return true;
	}
}